package com.et114.modules.order.service;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.et114.components.simpletable.DataList;
import com.et114.core.common.CONST;
import com.et114.core.services.impl.BaseServicesImpl;
import com.et114.core.utility.DateUtil;
import com.et114.core.utility.FileUtils;
import com.et114.core.utility.StringPool;
import com.et114.modules.cust.BIZ;
import com.et114.modules.cust.dao.IMng_CustDao;
import com.et114.modules.cust.vo.CustBaseVo;
import com.et114.modules.cust.vo.CustInfoVo;
import com.et114.modules.order.dao.IMng_OrderDao;
import com.et114.modules.order.vo.OrderTrack;
import com.et114.modules.order.vo.OrderVO;
import com.et114.modules.products.base.ProImage;
import com.et114.modules.products.commons.Const;
import com.et114.modules.products.commons.Context;
import com.et114.modules.products.supplier.services.SupplierServiceImpl;
import com.et114.modules.products.supplier.vo.ProSuppliers;
import com.et114.modules.system.dictionary.DictionaryCache;
import com.et114.modules.system.dictionary.vo.BizOption;
import com.et114.modules.system.dictionary.vo.BizType;

/**
 * @author zhiweip
 * @version 1.0
 * @created 24-十一月-2010 23:51:43
 */
public class Mng_OrderServiceImp extends BaseServicesImpl implements IMng_OrderService {
	
	public IMng_OrderDao		m_IMng_OrderDao;
	private IMng_CustDao		mng_CustDao;
	private SupplierServiceImpl	supplierServiceImpl;
	private String				lastfilename	= "";
	private static String		oldno			= DateUtil.formatDate (
														new Date ( ) ,
														"yyyyMMddHHmmss" );	;
	
	public void setM_IMng_OrderDao ( IMng_OrderDao mng_OrderDao ) {
		m_IMng_OrderDao = mng_OrderDao;
	}
	
	public void setMng_CustDao ( IMng_CustDao mng_CustDao ) {
		this.mng_CustDao = mng_CustDao;
	}
	
	/**
	 * 新增/更新
	 * 
	 * @param vo
	 * @throws Exception
	 */
	public Integer updateOrder ( OrderTrack order , HttpServletRequest request )
			throws Exception {
		// 以下信息存在则,不存在则新增
		// //客户信息: 信用卡,寄送地址,发票信息更新或新增
		// //快递方式:免费/两天/隔天可选择，可编辑
		// //快递公司

	/*
		String itemtype = order.getItemtype ( ) ; 
		if ( BIZ.TYPES_TEL.equals ( itemtype ) && order.getItemkindID ( ) != null ) {
			List < ProImage > proImages = Context
				.getProImageServiceImpl ( )
				.getProImageList ( 
					new ProImage ( Long.valueOf ( order.getItemid ( ) ) , Byte.valueOf ( Const.PRO_TYPE_CELLPHONE ) , Byte.valueOf ( order.getItemkindID ( ) ) ) );
			if ( proImages.size ( ) > 0 ) 
				order.setItemimg ( proImages.get ( 0 ).getPropsm ( ) ) ; 

		} else if ( BIZ.TYPES_PC.equals ( itemtype ) && order.getItemkindID ( ) != null ) { 
			List < ProImage > proImages = Context
				.getProImageServiceImpl ( )
				.getProImageList ( 
					new ProImage ( Long.valueOf ( order.getItemid ( ) ) , Byte.valueOf ( Const.PRO_TYPE_NETBOOK ) , Byte.valueOf ( order.getItemkindID ( ) ) )  );
			if ( proImages.size ( ) > 0 ) 
				order.setItemimg ( proImages.get ( 0 ).getPropsm ( ) ) ;
			
		} */
		
		
		
		
		if ( order.getOrderid ( ) != null ) {
			if ( order.getDetailid ( ) != null ) {
				if ( order.getCursupplyid ( ) == null
						|| order.getCursupplyid ( ).equals ( "" ) ) {
					order.setCursupplyid ( "-1" );
				}
				m_IMng_OrderDao.updateOrderDetail ( order );
				OrderTrack vo = new OrderTrack ( );
				vo.setOrderid ( order.getOrderid ( ) );
				vo.setOrdrmk ( order.getOrdrmk ( ) );
				vo.setFixplandate ( order.getFixplandate ( ) );
				vo.setFixplantime ( order.getFixplantime ( ) );
				vo.setOrdemp ( order.getOrdemp ( ) );
				
				m_IMng_OrderDao.updateOrder ( vo );
				
			} else if ( order.getInfoid ( ) != null ) {
				
				saveOrderInfo ( order , request );
				OrderTrack vo = new OrderTrack ( );
				vo.setOrderid ( order.getOrderid ( ) );
				vo.setPayway ( order.getPayway ( ) );
				vo.setOrdemp ( order.getOrdemp ( ) );
				m_IMng_OrderDao.updateOrder ( vo );
				m_IMng_OrderDao.updateETOrdreInfo ( order );
				
			} else {
				String newItemimgname = copySaveImg ( order );
				order.setItemimg ( newItemimgname );
				if(null == order.getIntegral ( ))
					order.setIntegral ( new Integer("0") );
				// order.setSerplan ( order.getFullDesc_serplan ( ) );
				m_IMng_OrderDao.insertOrder_Detail ( order );
				
			}
			return order.getOrderid ( );
		}
		
		if ( order.getSalenum ( ) == null )
			order.setSalenum ( new Integer ( "1" ) );
		
		// -----------------------------------------------------------
		saveOrderInfo ( order , request );
		
		order.setOrderstate ( BIZ.ORDER_OK );
		if ( order.getOrderdate ( ) == null
				|| order.getOrderdate ( ).length ( ) < 8 ) {
			order.setOrderdate ( DateUtil.getDateOutUSA ( null , false ) );
		}
		
		String orderno = order.getItemtype ( ) + "" + getSeriNo ( );
		order.setOrderno ( orderno );
		order.setIsfix ( "0" );
		if ( order.getFixdate ( ) != null )
			order.setIsfix ( "1" );
		Integer orderid = m_IMng_OrderDao.insertOrder ( order );
		order.setOrderid ( orderid );
		String newItemimgname = copySaveImg ( order );
		order.setItemimg ( newItemimgname );
		if(null == order.getIntegral ( ))
			order.setIntegral ( new Integer("0") );
		// order.setSerplan ( order.getFullDesc_serplan ( ) );
		m_IMng_OrderDao.insertOrder_Detail ( order );
		
		if ( order.getSupid ( ) != null
				&& order.getSupid ( ).trim ( ).equals ( "" ) ) {
			order.setSupid ( null );
		}
		m_IMng_OrderDao.insertOrderInfo ( order );
		
		return orderid;
	}
	
	private String copySaveImg ( OrderTrack order ) {
		
		String newItemimgname = "views/et114-img/" + order.getItemtype ( );
		if ( lastfilename != null
				&& lastfilename.equals ( order.getItemimg ( ) ) ) {
			return lastfilename;
		}
		
		order.getItemid ( ) ; 
		
		if (  order.getItemimg ( ) == null ) return newItemimgname ;
		try {
			String newpath = "";
			ClassLoader loader = super.getClass ( ).getClassLoader ( );
			String path = loader.getResource ( "" ).getPath ( );
			
			int e = loader.getResource ( "" ).getPath ( ).indexOf ( "WEB-INF" );
			path = path.substring ( 1 , e );
			
			File oldfile = new File ( path + order.getItemimg ( ) );
			String filename = FileUtils.copy2 ( oldfile ,
					path + newItemimgname , false );
			if ( null != filename ) {
				newItemimgname = "/" + newItemimgname + "/" + filename;
			}
			lastfilename = newItemimgname;
			return newItemimgname;
		} catch ( Exception e ) {
			e.printStackTrace ( );
		}
		newItemimgname = order.getItemimg ( );
		return newItemimgname;
	}
	
	private String getSeriNo ( ) {
		
		String nowno = DateUtil.formatDate ( new Date ( ) , "yyyyMMddHHmmss" );
		// System.out.println (oldno+"=="+nowno);
		if ( oldno.equals ( nowno ) ) {
			nowno = String.valueOf ( Long.parseLong ( nowno ) + 1 );
		}
		oldno = nowno;
		return nowno;
	}
	
	private void saveOrderInfo ( OrderTrack order , HttpServletRequest request )
			throws Exception {
		if ( "C".equalsIgnoreCase ( order.getPayway ( ) )
				&& ! request.getParameter ( "cret_lid" ).equals ( "-1" ) ) {
			String cret_no = request.getParameter ( "cret_no" );
			String cret_expire = request.getParameter ( "cret_expire" );
			String cret_code = request.getParameter ( "cret_code" );
			String cret_ower = request.getParameter ( "cret_ower" );
			String cret_billaddr = request.getParameter ( "cret_billaddr" );
			String cret_city = request.getParameter ( "cret_city" );
			String cret_state = request.getParameter ( "cret_state" );
			String cret_zip = request.getParameter ( "cret_zip" );
			String cret_lid = request.getParameter ( "cret_lid" );
			order.setInvno ( null );
			
			// System.out.println ("--cret_lid---------:"+cret_lid);
			if ( ( cret_lid == null || "".equals ( cret_lid ) )
					&& ( cret_no != null && ! cret_no.equals ( "" ) ) ) {
				CustInfoVo custinf = new CustInfoVo ( );
				custinf.setDtype ( BIZ.INFO_CERT );
				custinf.setCustid ( order.getCustid ( ) );
				custinf.setBillcode ( cret_code );
				custinf.setBillno ( cret_no );
				custinf.setOwner ( cret_ower );
				custinf.setExpire ( cret_expire );
				custinf.setBilladd ( cret_billaddr );
				custinf.setState ( cret_state );
				custinf.setCity ( cret_city );
				custinf.setZipcode ( cret_zip );
				List cclist = mng_CustDao.loadCustMsgByType ( custinf );
				if ( cclist == null || cclist.size ( ) < 1 )
					mng_CustDao.insertCustMsg ( custinf );
				custinf = null;
			} else if ( cret_lid != null && ! "".equals ( cret_lid ) ) {// 重新获取信用卡
			
				CustInfoVo custinf = new CustInfoVo ( );
				custinf.setCustid ( order.getCustid ( ) );
				custinf.setLid ( new Integer ( cret_lid ) );
				List < CustInfoVo > list = mng_CustDao
						.loadCustMsgByType ( custinf );
				if ( list.size ( ) < 1 ) {
					throw new Exception ( BIZ.ERROR_ORD_CRET );
				}
				custinf = list.get ( 0 );
				cret_no = custinf.getBillno ( );
				cret_expire = custinf.getExpire ( );
				cret_ower = custinf.getOwner ( );
				cret_code = custinf.getBillcode ( );
				cret_billaddr = custinf.getBilladd ( );
				cret_city = custinf.getCity ( );
				cret_state = custinf.getState ( );
				cret_zip = custinf.getZipcode ( );
			}
			
			order.setCardno ( cret_no );
			order.setBillexpire ( cret_expire );
			order.setBillcode ( cret_code );
			order.setBillowner ( cret_ower );
			order.setBilladd ( cret_billaddr );
			order.setBillcity ( cret_city );
			order.setBillstate ( cret_state );
			order.setBillzip ( cret_zip );
			
		} else if ( "B".equalsIgnoreCase ( order.getPayway ( ) ) ) {
			String bill_name = request.getParameter ( "bill_name" );
			String bill_bankno = request.getParameter ( "bill_bankno" );
			String bill_no = request.getParameter ( "bill_no" );
			String bill_code = request.getParameter ( "bill_code" );
			String bill_ower = request.getParameter ( "bill_ower" );
			String bill_addr = request.getParameter ( "bill_addr" );
			String bill_city = request.getParameter ( "bill_city" );
			String bill_state = request.getParameter ( "bill_state" );
			String bill_zip = request.getParameter ( "bill_zip" );
			String bill_lid = request.getParameter ( "bill_lid" );
			
			order.setBillname ( bill_name );
			order.setBillbankno ( bill_bankno );
			order.setCardno ( bill_no );
			order.setBillcode ( bill_code );
			order.setBillowner ( bill_ower );
			order.setBilladd ( bill_addr );
			order.setBillcity ( bill_city );
			order.setBillstate ( bill_state );
			order.setBillzip ( bill_zip );
			
			if ( ( bill_lid == null || bill_lid.equals ( "" ) )
					&& ( bill_bankno != null && ! bill_bankno.equals ( "" ) ) ) {
				CustInfoVo custinf = new CustInfoVo ( );
				custinf.setDtype ( BIZ.INFO_BILL );
				custinf.setCustid ( order.getCustid ( ) );
				custinf.setComname ( bill_name );
				custinf.setBankno ( bill_bankno );
				custinf.setBillno ( bill_no );
				custinf.setOwner ( bill_ower );
				custinf.setAddress ( bill_addr );
				custinf.setState ( bill_state );
				custinf.setCity ( bill_city );
				custinf.setZipcode ( bill_zip );
				custinf.setBillcode ( bill_code );
				List cclist = mng_CustDao.loadCustMsgByType ( custinf );
				if ( cclist == null || cclist.size ( ) < 1 )
					mng_CustDao.insertCustMsg ( custinf );
				custinf = null;
			}
			
		} else if ( "-1".equals ( request.getParameter ( "cret_lid" ) ) ) {
			// 没有修改不做操作
		}
		
		String lic_lid = request.getParameter ( "lic_lid" );
		// System.out.println ("====lic_lid======|"+lic_lid+"|");
		if ( ( lic_lid == null || lic_lid.trim ( ).equals ( "" ) )
				&& ( order.getLicno ( ) != null && ! order.getLicno ( ).equals (
						"" ) ) ) {
			CustInfoVo custinf = new CustInfoVo ( );
			custinf.setDtype ( BIZ.INFO_LIC );
			custinf.setCustid ( order.getCustid ( ) );
			custinf.setBillno ( order.getLicno ( ) );
			custinf.setClevel ( order.getLiclever ( ) );
			custinf.setExpire ( order.getLicexpire ( ) );
			custinf.setAddress ( order.getLicaddr ( ) );
			custinf.setState ( order.getLicstate ( ) );
			custinf.setCity ( order.getLiccity ( ) );
			custinf.setZipcode ( order.getLiczip ( ) );
			List cclist = mng_CustDao.loadCustMsgByType ( custinf );
			if ( cclist == null || cclist.size ( ) < 1 )
				mng_CustDao.insertCustMsg ( custinf );
			custinf = null;
		} else if ( ! "-1".equals ( lic_lid ) && null != ( lic_lid )
				&& ! lic_lid.trim ( ).equals ( "" ) ) {
			CustInfoVo custinf = new CustInfoVo ( );
			custinf.setCustid ( order.getCustid ( ) );
			custinf
					.setLid ( new Integer ( request.getParameter ( "lic_lid" ) ) );
			List < CustInfoVo > list = mng_CustDao.loadCustMsgByType ( custinf );
			if ( list.size ( ) < 1 ) {
				throw new Exception ( BIZ.ERROR_ORD_CUST );
			}
			custinf = list.get ( 0 );
			order.setLicno ( custinf.getBillno ( ) );
			order.setLiclever ( custinf.getClevel ( ) );
			order.setLicexpire ( custinf.getExpire ( ) );
			order.setLicaddr ( custinf.getAddress ( ) );
			order.setLicstate ( custinf.getState ( ) );
			order.setLiccity ( custinf.getCity ( ) );
			order.setLiczip ( custinf.getZipcode ( ) );
			
		}
		
		String link_lid = request.getParameter ( "link_lid" );
		if ( ( link_lid == null || link_lid.length ( ) < 1 )
				&& ( order.getPostcom ( ) != null && ! order.getPostcom ( )
						.equals ( "" ) ) ) {
			CustInfoVo custinf = new CustInfoVo ( );
			custinf.setCustid ( order.getCustid ( ) );
			custinf.setDtype ( BIZ.INFO_LINK );
			custinf.setComname ( order.getPostcom ( ) );
			custinf.setBankno ( order.getPosttax ( ) );
			custinf.setBillcode ( order.getPosttel ( ) );
			custinf.setAddress ( order.getPostaddr ( ) );
			custinf.setState ( order.getPoststate ( ) );
			custinf.setCity ( order.getPostcity ( ) );
			custinf.setZipcode ( order.getPostzip ( ) );
			List cclist = mng_CustDao.loadCustMsgByType ( custinf );
			if ( cclist == null || cclist.size ( ) < 1 )
				mng_CustDao.insertCustMsg ( custinf );
			custinf = null;
		} else if ( null != link_lid && link_lid.length ( ) > 0
				&& ! request.getParameter ( "link_lid" ).equals ( "-1" ) ) {
			CustInfoVo custinf = new CustInfoVo ( );
			custinf.setCustid ( order.getCustid ( ) );
			custinf.setLid ( new Integer ( link_lid ) );
			List < CustInfoVo > list = mng_CustDao.loadCustMsgByType ( custinf );
			if ( list.size ( ) < 1 ) {
				throw new Exception ( BIZ.ERROR_ORD_LGI );
			}
			custinf = list.get ( 0 );
			order.setPostcom ( custinf.getComname ( ) );
			order.setPosttax ( custinf.getBankno ( ) );
			order.setPosttel ( custinf.getBillcode ( ) );
			order.setPostaddr ( custinf.getAddress ( ) );
			order.setPoststate ( custinf.getState ( ) );
			order.setPostcity ( custinf.getCity ( ) );
			order.setPostzip ( custinf.getZipcode ( ) );
			
		}
		
		String sb_lid = request.getParameter ( "sb_lid" );
		if ( sb_lid == null || sb_lid.equals ( "" ) ) {
			CustInfoVo custinf = new CustInfoVo ( );
			custinf.setCustid ( order.getCustid ( ) );
			custinf.setDtype ( BIZ.INFO_LINK );
			custinf.setComname ( order.getSbcom ( ) );
			custinf.setBankno ( order.getSbilltax ( ) );
			custinf.setBillcode ( order.getSbilltel ( ) );
			custinf.setAddress ( order.getSbilladd ( ) );
			custinf.setState ( order.getSbillstate ( ) );
			custinf.setCity ( order.getSbillcity ( ) );
			custinf.setZipcode ( order.getSbillzip ( ) );
			List cclist = mng_CustDao.loadCustMsgByType ( custinf );
			if ( cclist == null || cclist.size ( ) < 1 )
				mng_CustDao.insertCustMsg ( custinf );
			custinf = null;
		} else {
			CustInfoVo custinf = new CustInfoVo ( );
			custinf.setCustid ( order.getCustid ( ) );
			custinf.setLid ( new Integer ( sb_lid ) );
			List < CustInfoVo > list = mng_CustDao.loadCustMsgByType ( custinf );
			if ( list.size ( ) < 1 ) {
				throw new Exception ( BIZ.ERROR_ORD_LGI );
			}
			custinf = list.get ( 0 );
			order.setSbcom ( custinf.getComname ( ) );
			order.setSbilltax ( custinf.getBankno ( ) );
			order.setSbilltel ( custinf.getBillcode ( ) );
			order.setSbilladd ( custinf.getAddress ( ) );
			order.setSbillstate ( custinf.getState ( ) );
			order.setSbillcity ( custinf.getCity ( ) );
			order.setSbillzip ( custinf.getZipcode ( ) );
		}
		
	}
	
	/**
	 * 
	 * @param vo
	 * @throws Exception
	 */
	public DataList queryOrderDataList ( OrderTrack vo ) throws Exception {
		
		//DictionaryCache bean = DictionaryCache.getInstance ( );
		//if ( null == vo.getLanguage ( ) || "".equals ( vo.getLanguage ( ) ) ) {
			vo.setLanguage ( CONST.COUNTRY_ZH );
		//}
		DataList dList = m_IMng_OrderDao.queryOrderDataList ( vo );
		
		List < OrderTrack > list = dList.getNowlist ( );
		for ( OrderTrack ot : list ) {
			ot.setIsaudi_desc ( getBizOptionName ( "ORD_ISAUDI" , ot
					.getIsaudi ( ) ,CONST.COUNTRY_ZH) );
			ot.setIstosup_desc ( getBizOptionName ( "ORD_ISTOSUP" , ot
					.getIstosup ( ) ,CONST.COUNTRY_ZH) );
			ot.setIslgi_desc ( getBizOptionName ( "ORD_ISLGI" , ot
					.getIslgi ( ) ,CONST.COUNTRY_ZH) );
			ot.setIsarrive_desc ( getBizOptionName ( "ORD_ISARRIVE" , ot
					.getIsarrive ( ),CONST.COUNTRY_ZH ) );
			ot.setIsset_desc ( getBizOptionName ( "ORD_ISSET" , ot
					.getIsset ( ) ,CONST.COUNTRY_ZH) );
			ot.setIssettle_desc ( getBizOptionName ( "ORD_SETTLE" , ot
					.getIssettle ( ) ,CONST.COUNTRY_ZH) );
			ot.setIswork_desc ( getBizOptionName ( "ORD_SETTLE_WORKER" ,
					ot.getIswork ( ) ,CONST.COUNTRY_ZH) );
			ot.setOrderstate_desc ( getBizOptionName ( "ORDSTATE" , ot
					.getOrderstate ( ) ,CONST.COUNTRY_ZH) );
			ot.setLanguage ( vo.getLanguage ( ) );
			currentOrderState ( ot );
			if ( null != ot.getOrderdetail ( ) ) {
				List < OrderTrack > ll = ot.getOrderdetail ( );
				for ( OrderTrack dt : ll ) {
					if ( null != dt.getSerplan ( ) )
						dt
								.setSerplanArr ( dt.getSerplan ( ).split (
										BIZ.sKey ) );
				}
			}
		}
		return dList;
	}
	
	private void currentOrderState ( OrderTrack ot ) throws Exception {
		DictionaryCache bean = DictionaryCache.getInstance ( );
		if ( ot.getOrderstate ( ).equals ( "1" ) ) {
			ot.setCurrent_desc ( getBizOptionName ( "ORDSTATE" , ot
					.getOrderstate ( ),ot.getLanguage ( ) ) );
			ot.setKey_a ( "DIS" );
		} else if ( ot.getIsaudi ( ).equals ( "0" ) ) {
			ot.setCurrent_desc ( getBizOptionName ( "ORD_ISAUDI" , ot
					.getIsaudi ( ),ot.getLanguage ( ) ) );
			ot.setKey_a ( "AUDI" );
		} else if ( ot.getIstosup ( ).equals ( "0" ) ) {
			ot.setCurrent_desc ( getBizOptionName ( "ORD_ISTOSUP" , ot
					.getIstosup ( ),ot.getLanguage ( ) ) );
			ot.setKey_a ( "TOSUP" );
		} else if ( ot.getIslgi ( ).equals ( "0" ) ) {
			ot.setCurrent_desc ( getBizOptionName ( "ORD_ISLGI" , ot
					.getIslgi ( ),ot.getLanguage ( ) ) );
			ot.setKey_a ( "LGI" );
		} else if ( ot.getIsarrive ( ).equals ( "0" ) ) {
			ot.setCurrent_desc ( getBizOptionName ( "ORD_ISARRIVE" , ot
					.getIsarrive ( ),ot.getLanguage ( ) ) );
			ot.setKey_a ( "LGI" );
		} else if ( ot.getIsset ( ).equals ( "0" ) ) {
			ot.setCurrent_desc ( getBizOptionName ( "ORD_ISSET" , ot
					.getIsset ( ),ot.getLanguage ( ) ) );
			ot.setKey_a ( "BIZ" );
		} else if ( ot.getIssettle ( ).equals ( "0" ) ) {
			ot.setCurrent_desc ( getBizOptionName ( "ORD_SETTLE" , ot
					.getIssettle ( ),ot.getLanguage ( ) ) );
			ot.setKey_a ( "SETTLE" );
		} else if ( ot.getIswork ( ).equals ( "0" ) ) {
			ot.setCurrent_desc ( getBizOptionName ( "ORD_SETTLE_WORKER" ,
					ot.getIswork ( ),ot.getLanguage ( ) ) );
			ot.setKey_a ( "SETTLE" );
		} else {
			ot
					.setCurrent_desc ( getBizOptionName ( "ORD_COMPLETE" ,
							"0" ,ot.getLanguage ( )) );
			ot.setKey_a ( "OK" );
		}
	}
	
	private void currentOrderStateCust ( OrderTrack ot ) throws Exception { 
		if ( ot.getOrderstate ( ).equals ( "1" ) ) {
			ot.setCurrent_desc ( getBizOptionName ( "ORDSTATE" , ot
					.getOrderstate ( ),ot.getLanguage ( ) ));
			ot.setKey_a ( "DIS" );
		} else if ( ot.getIsaudi ( ).equals ( "0" ) ) {
			ot.setCurrent_desc ( getBizOptionName ( "ORD_ISAUDI" , ot
					.getIsaudi ( ) ,ot.getLanguage ( )));
			ot.setKey_a ( "AUDI" );
		} else {
			if ( ot.getIsarrive ( ).equals ( "0" ) ) {
				ot.setCurrent_desc ( getBizOptionName ( "ORD_ISARRIVE" ,
						ot.getIsarrive ( ) ,ot.getLanguage ( )) );
				ot.setKey_a ( "LGI" );
			} else if ( ot.getIsset ( ).equals ( "1" ) ) {
				ot.setCurrent_desc ( getBizOptionName ( "ORD_COMPLETE" ,
						"0",ot.getLanguage ( )) );
				ot.setKey_a ( "OK" );
			}
		}
		String lang=ot.getLanguage ( );
		String s = str ( ot.getIsaudi ( ) )
		+ str ( ot.getIstosup ( ) ) + str ( ot.getIslgi ( ) )
		+ str ( ot.getIsarrive ( ) ) + str ( ot.getIsset ( ) );
		if ( s.equals ( "00000" ) ) {
			ot.setCurrent_desc ( getBizOptionName ( "ORD_ISAUDI" ,
					"0" ,lang) );
		} else if ( s.equals ( "10000" ) ) {
			ot.setCurrent_desc ( getBizOptionName ( "ORD_ISAUDI" ,
					"1",lang ) );
		} else if ( s.equals ( "11000" ) ) {
			ot.setCurrent_desc ( getBizOptionName ( "ORD_ISTOSUP" ,
					"1",lang ) );
		} else if ( s.equals ( "11100" ) ) {
			ot.setCurrent_desc ( getBizOptionName ( "ORD_ISLGI" ,
					"1",lang ) );
		} else if ( s.equals ( "11110" ) ) {
			ot.setCurrent_desc ( getBizOptionName ( "ORD_ISARRIVE" ,
					"1" ,lang) );
		} else if ( s.equals ( "11111" ) ) {
			ot.setCurrent_desc ( getBizOptionName ( "ORD_ISSET" ,
					"1",lang ) );
		} else {
			ot.setCurrent_desc ( "-" );
		}
	}
	
	public String getBizOptionName ( String biztypekey , String bizoptionvalue,String language )
			throws Exception {
		DictionaryCache bean = DictionaryCache.getInstance ( );
		BizType bizType = bean.getBizType ( biztypekey );
		for ( Iterator iter = bizType.getOptions ( ).iterator ( ) ; iter
				.hasNext ( ) ; ) {
			BizOption option = ( BizOption ) iter.next ( );
			if (option.getLanguage ( ).equals ( language ) &&
				option.getOptionValue ( ).equals ( bizoptionvalue ) ) {
				return option.getOptionName ( );
			}
		} 
		return "";
	}
	
	public List querySupOrderList ( String [ ] orderids ) {
		return null;// m_IMng_OrderDao.querySupOrderList ( orderids );
	}
	
	public Long countIstoSup ( OrderVO vo ) {
		return null;// m_IMng_OrderDao.countIstoSup ( vo );
	}
	
	public void updateAudiOrder ( OrderTrack vo , String orderids )
			throws Exception {
		OrderTrack order = new OrderTrack ( );
		order.setAudirmk ( vo.getAudirmk ( ) );
		order.setIsaudi ( vo.getIsaudi ( ) );
		order.setAudiemp ( vo.getAudiemp ( ) );
		
		order.setAudidate ( DateUtil.getDateOutUSA ( null , false ) );
		
		order.setOperType ( BIZ.ORDER_OK );// 只能操作未作废的订单
		
		String [ ] ordids = orderids.split ( ";" );
		for ( String id : ordids ) {
			if ( id.length ( ) < 1 )
				continue;
			order.setOrderid ( new Integer ( id ) );
			m_IMng_OrderDao.updateOrder ( order );
		}
	}
	
	/**
	 * 供应商订单回录
	 */
	public void updateSupOrder ( OrderTrack vo , String orderids )
			throws Exception {
		OrderTrack order = new OrderTrack ( );
		order.setSupemp ( vo.getSupemp ( ) );
		order.setSupnum ( vo.getSupnum ( ) );
		order.setTosupdate ( vo.getTosupdate ( ) );
		order.setSupordno ( vo.getSupordno ( ) );
		order.setUpdtUser ( vo.getUpdtUser ( ) );
		order.setSupemail ( vo.getSupemail ( ) );
		order.setSuphost ( vo.getSuphost ( ) );
		order.setSecurtkey ( vo.getSecurtkey ( ) );
		
		order.setIstosup ( "1" );
		order.setOperType ( "0" );// 只能操作未作废的订单
		
		String [ ] ordids = orderids.split ( ";" );
		for ( String id : ordids ) {
			if ( id.length ( ) < 1 )
				continue;
			order.setOrderid ( new Integer ( id ) );
			m_IMng_OrderDao.updateOrder ( order );
			
		}
	}
	
	public void updateBizOrder ( OrderTrack vo , String orderids )
			throws Exception {
		OrderTrack order = new OrderTrack ( );
		order.setSetdate ( vo.getSetdate ( ) );
		order.setSetemp ( vo.getSetemp ( ) );
		order.setOperType ( "0" );// 只能操作未作废的订单
		order.setIsset ( "1" );
		
		order.setUpdtUser ( vo.getUpdtUser ( ) );
		
		String [ ] ordids = orderids.split ( ";" );
		for ( String id : ordids ) {
			if ( id.length ( ) < 1 )
				continue;
			order.setOrderid ( new Integer ( id ) );
			m_IMng_OrderDao.updateOrder ( order );
		}
		
	}
	
	public void updateBizOrder_S ( OrderTrack vo , String orderids )
			throws Exception {
		OrderTrack order = new OrderTrack ( );
		order.setDuedate ( vo.getDuedate ( ) );
		order.setFircheckup ( vo.getFircheckup ( ) );
		order.setFircheckupemp ( vo.getFircheckupemp ( ) );
		order.setIsfircheck ( "1" );
		order.setOperType ( "0" );// 只能操作未作废的订单
		
		order.setUpdtUser ( vo.getUpdtUser ( ) );
		
		String [ ] ordids = orderids.split ( ";" );
		for ( String id : ordids ) {
			if ( id.length ( ) < 1 )
				continue;
			order.setOrderid ( new Integer ( id ) );
			m_IMng_OrderDao.updateOrder ( order );
		}
		
	}
	
	public void updateLgiOrder ( OrderTrack vo , String orderids )
			throws Exception {
		OrderTrack order = new OrderTrack ( );
		order.setLgicom ( vo.getLgicom ( ) );
		order.setLgiemp ( vo.getLgiemp ( ) );
		order.setLgino ( vo.getLgino ( ) );
		order.setLgiway ( vo.getLgiway ( ) );
		order.setUpdtUser ( vo.getUpdtUser ( ) );
		order.setLgidate ( vo.getLgidate ( ) );
		order.setIslgi ( "1" );
		order.setOperType ( "0" );// 只能操作未作废的订单
		
		String [ ] ordids = orderids.split ( ";" );
		for ( String id : ordids ) {
			if ( id.length ( ) < 1 )
				continue;
			order.setOrderid ( new Integer ( id ) );
			m_IMng_OrderDao.updateOrder ( order );
		}
		
	}
	
	public void updateLgiOrder_Arr ( OrderTrack vo , String orderids )
			throws Exception {
		OrderTrack order = new OrderTrack ( );
		order.setArremp ( vo.getArremp ( ) );
		order.setArrivedate ( vo.getArrivedate ( ) );
		order.setOperType ( "0" );// 只能操作未作废的订单
		
		order.setUpdtUser ( vo.getUpdtUser ( ) );
		if ( order.getArrivedate ( ) != null ) {
			order.setIsarrive ( "1" );
		}
		if ( order.getFircheckup ( ) != null ) {
			order.setIsset ( "1" );
		} else {
			order.setIsset ( "0" );
		}
		
		String [ ] ordids = orderids.split ( ";" );
		for ( String id : ordids ) {
			if ( id.length ( ) < 1 )
				continue;
			order.setOrderid ( new Integer ( id ) );
			OrderTrack ppvo = new OrderTrack ( );
			ppvo.setOrderid ( new Integer ( id ) );
			Object oo = m_IMng_OrderDao.loadOrder ( ppvo );
			if ( null != oo ) {
				ppvo = ( OrderTrack ) oo;
				if ( BIZ.TYPES_SAFE.equals ( ppvo.getItemtype ( ) )
						|| BIZ.TYPES_TV.equals ( ppvo.getItemtype ( ) ) ) {
					// order.setLgidate ( vo.getArrivedate ( ) );
					order.setIslgi ( "1" );
				}
			}
			m_IMng_OrderDao.updateOrder ( order );
		}
		
	}
	
	public void updateSettleOrder ( OrderTrack vo , String orderids )
			throws Exception {
		OrderTrack order = new OrderTrack ( );
		order.setUpdtUser ( vo.getUpdtUser ( ) );
		order.setSettledate ( vo.getSettledate ( ) );
		order.setSettleemp ( vo.getSettleemp ( ) );
		order.setSettlesum ( vo.getSettlesum ( ) );
		order.setIssettle ( "1" );
		
		String [ ] ordids = orderids.split ( ";" );
		for ( String id : ordids ) {
			if ( id.length ( ) < 1 )
				continue;
			
			order.setOrderid ( new Integer ( id ) );
			m_IMng_OrderDao.updateOrder ( order );
			
			OrderTrack oop = new OrderTrack ( );
			oop.setOrderid ( new Integer ( id ) );
			oop = ( OrderTrack ) m_IMng_OrderDao.loadOrder ( oop );
			CustBaseVo cust = new CustBaseVo ( );
			cust.setCustid ( oop.getCustid ( ) );
			cust.setCusttype ( BIZ.CUST_TRADE );
			mng_CustDao.updateCustBaseInfo ( cust );
			m_IMng_OrderDao.update_Pub_CustIntegral_By_order(oop);
		}
		
	}
	
	public void updateSettleOrder_W ( OrderTrack vo , String orderids )
			throws Exception {
		OrderTrack order = new OrderTrack ( );
		order.setUpdtUser ( vo.getUpdtUser ( ) );
		order.setWorkdate ( vo.getWorkdate ( ) );
		order.setWorkemp ( vo.getWorkemp ( ) );
		
		order.setIswork ( "1" );
		order.setWorksum ( vo.getWorksum ( ) );
		
		String [ ] ordids = orderids.split ( ";" );
		for ( String id : ordids ) {
			if ( id.length ( ) < 1 )
				continue;
			order.setOrderid ( new Integer ( id ) );
			m_IMng_OrderDao.updateOrder ( order );
		}
		
	}
	
	public void updateQuitOrder ( OrderTrack vo , String orderids )
			throws Exception {
		OrderTrack order = new OrderTrack ( );
		order.setUpdtUser ( vo.getUpdtUser ( ) );
		order.setCanceldate ( vo.getCanceldate ( ) );
		order.setCancelemp ( vo.getCancelemp ( ) );
		order.setCancelreson ( vo.getCancelreson ( ) );
		order.setOrderstate ( "1" );
		order.setOperType ( "0" );// 只能操作未作废的订单
		
		String [ ] ordids = orderids.split ( ";" );
		for ( String id : ordids ) {
			if ( id.length ( ) < 1 )
				continue;
			order.setOrderid ( new Integer ( id ) );
			m_IMng_OrderDao.updateOrder ( order );
		}
		
	}
	
	public int checkIsSameITEMTYPE ( String [ ] ids ) {
		return m_IMng_OrderDao.checkIsSameITEMTYPE ( ids );
	}
	
	public Object loadOrderTrackBy_Orderid ( OrderTrack vo ) {
		if ( null == vo.getLanguage ( ) || "".equals ( vo.getLanguage ( ) ) ) {
			vo.setLanguage ( CONST.COUNTRY_ZH );
		}
		return m_IMng_OrderDao.loadOrder ( vo );
	}
	
	/**
	 * 
	 * @param vo
	 */
	public Object loadOrder ( OrderTrack vo ) {
		if ( null == vo.getLanguage ( ) || "".equals ( vo.getLanguage ( ) ) ) {
			vo.setLanguage ( CONST.COUNTRY_ZH );
		}
		OrderTrack order = ( OrderTrack ) m_IMng_OrderDao.loadOrder ( vo );
		try {
			currentOrderState ( order );
		} catch ( Exception e ) {
			e.printStackTrace ( );
		}
		return order;
	}
	
	public Object viewOrder ( OrderTrack vo ) {
		if ( null == vo.getLanguage ( ) || "".equals ( vo.getLanguage ( ) ) ) {
			vo.setLanguage ( CONST.COUNTRY_ZH );
		}
		String lang=vo.getLanguage ( );
		OrderTrack order = ( OrderTrack ) m_IMng_OrderDao.viewOrder ( vo );
		//System.out.println ("======s=======:"+vo.getLanguage ( )+"lang:"+ lang);
		try {
			order.setLanguage ( lang );
			if ( "cust".equals ( vo.getOperType ( ) ) ) {
				currentOrderStateCust ( order );
			} else
				currentOrderState ( order );
		} catch ( Exception e ) {
			e.printStackTrace ( );
		}
		return order;
	}
	
	public List loadOrderDetail ( OrderTrack vo ) {
		if ( null == vo.getLanguage ( ) || "".equals ( vo.getLanguage ( ) ) ) {
			vo.setLanguage ( CONST.COUNTRY_ZH );
		}
		List < OrderTrack > dList = m_IMng_OrderDao.loadOrderDetail ( vo );
		
		try {
			ProSuppliers psvo = new ProSuppliers ( );
			psvo.setSupplierPros ( StringPool.COMMA + Const.PRO_TYPE_NETBOOK
					+ StringPool.COMMA );
			psvo.setWorkFlg ( String.valueOf ( Const.COMMON_STATUS_ENA ) );
			List < ProSuppliers > proTypes;
			
			proTypes = supplierServiceImpl.querySuppliersList ( psvo );
			for ( OrderTrack ot : dList ) {
				if ( ot.getCursupplyid ( ) != null ) {
					for ( ProSuppliers pro : proTypes ) {
						if ( pro.getProSupplierId ( ).toString ( ).equals (
								ot.getCursupplyid ( ) ) ) {
							ot.setCursupply ( pro.getProSupplierName ( ) );
						}
					}
				}
				if ( null != ot.getSerplan ( ) ) {
					ot.setSerplanArr ( ot.getSerplan ( ).split ( BIZ.sKey ) );
				}
			}
		} catch ( Exception e ) {
			e.printStackTrace ( );
		}
		return dList;
	}
	
	public Object loadOrderInfo ( OrderTrack vo ) {
		if ( null == vo.getLanguage ( ) || "".equals ( vo.getLanguage ( ) ) ) {
			vo.setLanguage ( CONST.COUNTRY_ZH );
		}
		OrderTrack info = ( OrderTrack ) m_IMng_OrderDao.loadOrderInfo ( vo );
		if ( null != info && "C".equalsIgnoreCase ( info.getPayway ( ) )
				&& null != info.getCardno ( ) ) {
			String no = info.getCardno ( );
			if ( no != null && no.length ( ) > 4 ) {
				no = "****-****-****-"
						+ no.substring ( no.length ( ) - 4 )
								.replace ( "-" , "" );
				info.setCardno ( no );
			} else {
				info.setCardno ( "****" );
			}
		}
		return info;
	}
	
	public int deleteOrderDetail ( OrderTrack vo ) {
		return m_IMng_OrderDao.delete_ET_ORDER_DETAILByKey ( vo );
	}
	
	public Object loadOrderdetail ( OrderTrack vo ) {
		if ( null == vo.getLanguage ( ) || "".equals ( vo.getLanguage ( ) ) ) {
			vo.setLanguage ( CONST.COUNTRY_ZH );
		}
		OrderTrack ot = ( OrderTrack ) m_IMng_OrderDao.loadOrderdetail ( vo );
		if ( null != ot.getSerplan ( ) ) {
			ot.setTempKey ( ot.getSerplan ( ).replace ( BIZ.sKey , ";" ) );
			ot.setSerplanArr ( ot.getSerplan ( ).split ( BIZ.sKey ) );
		}
		return ot;
	}
	
	private String str ( Object o ) {
		if ( o == null )
			return "-";
		else
			return o.toString ( );
	}
	
	public void setSupplierServiceImpl ( SupplierServiceImpl supplierServiceImpl ) {
		this.supplierServiceImpl = supplierServiceImpl;
	}
	
	public Map rep_order_Emp ( OrderTrack vo ) throws Exception {
		List < HashMap > dList = m_IMng_OrderDao.rep_order_Emp ( vo );
		// ORD.ORDERSTATE,emp.EMPID,ORD.ITEMTYPE,SUM(B.SALENUM) SALENUM
		//System.out.println ( dList.size ( ) );
		Map < String , String > rMap = new HashMap ( );
		for ( HashMap map : dList ) {
			String key = str ( map.get ( "ITEMTYPE" ) ) + "_"
					+ str ( map.get ( "EMPID" ) ) + "_"
					+ str ( map.get ( "ORDERSTATE" ) );
			//System.out.println ( "===key:" + key + " v:" + str ( map.get ( "SALENUM" ) ) );
			rMap.put ( key , str ( map.get ( "SALENUM" ) ) );
		}
		dList = null;
		return rMap;
	}
	
	public Map rep_order_Sup ( OrderTrack vo ) throws Exception {
		List < HashMap > dList = m_IMng_OrderDao.rep_order_Sup ( vo );
		// ORD.ORDERSTATE,emp.EMPID,ORD.ITEMTYPE,SUM(B.SALENUM) SALENUM
		//System.out.println ( dList.size ( ) );
		Map < String , String > rMap = new HashMap ( );
		for ( HashMap map : dList ) {
			String key = str ( map.get ( "ITEMTYPE" ) ) + "_"
					+ str ( map.get ( "SUPID" ) )
					; 
			//System.out.println ( "===key:" + key+"_SUP"+"//"+str ( map.get ( "SETTLESUM" ) ) );
			rMap.put ( key+"_SUP" , str ( map.get ( "SETTLESUM" ) ) );
			rMap.put ( key+"_EMP" , str ( map.get ( "WORKSUM" ) ) );
		}
		dList = null;
		return rMap;
	}
	public List getOrd_ProSuppliers ( com.et114.modules.products.supplier.vo.ProSuppliers vo ) throws Exception { 
		return m_IMng_OrderDao.getOrd_ProSuppliers( vo);
	}
}